package com.itboye.ihomebank.me;

import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.AreaAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.AreaBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.db.CityNoDBManager;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

public class ChooseAreaActivity extends BaseOtherActivity implements Observer {

	EditText edit_txt;
	HousePresenter housePresenter;
	private String max;
	private String city;
	private String kword;
	private String uid;
	ListView listView;
	TextView select_city, nodata;
	CityNoDBManager cityNoDBManager;
	private SQLiteDatabase database;
	private String mCityNames;
	ImageView close_icon;
	private List<AreaBean> list;
	@Override	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.close_icon:
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	protected int layout_id() {
		// TODO Auto-generated method stub
		return R.layout.activity_choose_area;
	}

	@Override
	protected void onInitController() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onInit() {
		// TODO Auto-generated method stub
		cityNoDBManager = new CityNoDBManager(this);
		cityNoDBManager.openDateBase();
		city = cityNoDBManager.queryCityNo("杭州市");
		select_city.setText("当前城市："
				+ SPUtils.get(this, null, SPContants.SELECT_CITY, ""));
		housePresenter = new HousePresenter(this);
		edit_txt.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
		edit_txt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_SEND
						|| (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
					// do something;
					uid = SPUtils.get(ChooseAreaActivity.this, null,
							SPContants.USER_ID, "") + "";
					kword = edit_txt.getText().toString();
					max = "10";
					housePresenter.getHouseArea(uid, kword, city, max);
					return true;
				}
				return false;
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.putExtra("xiaoqu", list.get(position).getCommunity());
				intent.putExtra("xiaoquName", list.get(position).getCommunityName());
				setResult(104, intent);
				finish();
			}
		});
	}

	@Override
	protected void onReadIntent(Bundle bundle) {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.getArea_success) {
				 list = (List<AreaBean>) entity.getData();
				if (list != null) {
					if (list.size() > 0) {
						listView.setAdapter(new AreaAdapter(this, list,
								R.layout.item_simple_list_left));
						listView.setVisibility(View.VISIBLE);
						nodata.setVisibility(View.GONE);
					} else {
						nodata.setVisibility(View.VISIBLE);
						listView.setVisibility(View.GONE);
					}
				} else {
					nodata.setVisibility(View.GONE);
					listView.setVisibility(View.VISIBLE);
				}
			} else if (entity.getEventType() == HousePresenter.getArea_fail) {
				ByAlert.alert(entity.getData());
			}

		}
	}

}
